package club.zhilin.guli.service.edu.controller.portal;

import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.base.dto.CourseDTO;
import club.zhilin.guli.service.edu.domain.Course;
import club.zhilin.guli.service.edu.domain.qo.PortalCourseQueryObject;
import club.zhilin.guli.service.edu.domain.vo.ChapterVO;
import club.zhilin.guli.service.edu.domain.vo.CourseRelation;
import club.zhilin.guli.service.edu.service.IChapterService;
import club.zhilin.guli.service.edu.service.ICourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhilin
 * @since 2022/2/14 22:07
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/portal/edu/course")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PortalCourseController {

    private final ICourseService iCourseService;
    private final IChapterService iChapterService;

    @ApiOperation("课程列表")
    @GetMapping("/list")
    public CommonResult<List<Course>> list(PortalCourseQueryObject portalCourseQueryObject) {
        return CommonResult.success(iCourseService.selectListByCondition(portalCourseQueryObject));
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("/course/{id}")
    public CommonResult<Map<String, Object>> detail(@PathVariable String id) {
        CourseRelation relation = iCourseService.selectCourseRelation(id);
        List<ChapterVO> vos = iChapterService.nestedList(id);
        Map<String, Object> result = new HashMap<>(2);
        result.put("relation", relation);
        result.put("vos", vos);
        return CommonResult.success(result);
    }

    @ApiOperation("通过课程id查询CourseDTO")
    @GetMapping("/inner/course/{courseId}")
    public CommonResult<CourseDTO> selectCourseDTO(@PathVariable("courseId") String courseId) {
        return CommonResult.success(iCourseService.selectCourseDTO(courseId));
    }

    @ApiOperation("通过课程id更新课程购买数量")
    @PutMapping("/update/course/{courseId}")
    public CommonResult<String> updateCourseBuyCount(@PathVariable String courseId) {
        iCourseService.updateBuyCountByCourseId(courseId);
        return CommonResult.success();
    }
}
